<?php
/**
 * JE Career package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );
jimport('joomla.html.pane');

class  hostingrajacareerViewcareer  extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

		$db 			=& JFactory::getDBO();
		$model			= & $this->getModel();
		JHTML::_('behavior.calendar');

		if($this->_layout == 'form') {
			$careerRecord 				=& $this->get('Data');
			$text 						= $careerRecord->id ? JText::_( 'JE_CAREER_EDIT' ) : JText::_( 'JE_CAREER_NEW' );
			JToolBarHelper::title(   JText::_( 'JE_CAREER' ) . ' - ' .  JText::_( 'JE_CURRENT_OPENINGS' ).': <small><small>[ ' . $text.' ]</small></small>', 'career_48.png' );
			JToolBarHelper::apply();
			JToolBarHelper::save();
			if ($careerRecord->id) {
				JToolBarHelper::cancel( 'cancel', 'Close' );
			} else {
				JToolBarHelper::cancel();
			}

			$this->assignRef('row',	$careerRecord);
			$category					= $model->getCategory();
			$ordering 	 	 			= $model->getOrdering();

			$lists['ordering']			= $ordering;
			$this->assignRef('lists',	    $lists);
			$this->assignRef('category',	$category);
			parent::display($tpl);
		} else {

			JToolBarHelper::title(   JText::_( 'JE_CAREER' ) . ' - ' .  JText::_( 'JE_CURRENT_OPENINGS' ), 'career_48.png' );
			JToolBarHelper::publish();
			JToolBarHelper::unpublish();
			JToolBarHelper::deleteListX(JText::_( 'JWD_WARN_DELETE' ));
			JToolBarHelper::editListX();
			JToolBarHelper::addNewX();

			$context			= 'com_hostingrajacareer.s.id_career.list.';
			$context			= 'com_hostingrajacareer.s.id_career.list.';
			$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		'c.category',	'cmd' );
			$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
			$filter_state		= $mainframe->getUserStateFromRequest( $context.'filter_state',		'filter_state',		'',				'word' );
			$search				= $mainframe->getUserStateFromRequest( $context.'search',			'search',			'',				'string' );
			$catid				= $mainframe->getUserStateFromRequest( $context.'catid',			'catid',			-1,				 'int' );
			$limit				= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'),   'int' );
			$limitstart 		= $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );

			$where = array();

			if ( $filter_state ) {
				if ( $filter_state == 'P' ) {
					$where[] 	= 's.state = 1';
				} else if ($filter_state == 'U' ) {
					$where[] 	= 's.state = 0';
				}
			}

			if ($search) {
				$where[]		= 'LOWER(s.position) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			}

			// Category filter
			if ($catid >= 0) {
				$where[] 		= 's.catid = ' . (int) $catid;
			}
			$where				= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
			if ($filter_order == 's.ordering') {
				$orderby 		= ' ORDER BY s.ordering '. $filter_order_Dir;
			} else {
				$orderby		= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', c.category, s.ordering';
			}

			// get the total number of records
			$query 		 		= 'SELECT count(*) FROM #__hostingraja_career AS s ' .
								   ' LEFT JOIN ' .
								   ' #__hostingraja_career_category AS c ON c.id = s.catid'
								  . $where ;
			$db->setQuery( $query );
			if (!$db->query()) {
				echo $db->getErrorMsg();
			}
			$total 	 			= $db->loadResult();
			jimport('joomla.html.pagination');
			$pageNav 			= new JPagination( $total, $limitstart, $limit );
			$query  			= ' SELECT s.*, c.category FROM #__hostingraja_career AS s ' .
								   ' LEFT JOIN ' .
								   ' #__hostingraja_career_category AS c ON c.id = s.catid'
									. $where
									. $orderby	;
			$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
			$rows				= $db->loadObjectList();

			// Get list of categories for dropdown filter
			$query 				= 'SELECT c.id AS value, c.category AS text ' .
								  ' FROM #__hostingraja_career_category AS c' .
								  ' ORDER BY c.ordering';

			$lists['catid'] 	= $model->filterCategory($query, $catid);
			// Categories list end.

			// State filter.
			$lists['state']		= JHTML::_('grid.state',  $filter_state );
			// State filter End.

			// Table ordering.
			$lists['order_Dir']	= $filter_order_Dir;
			$lists['order']		= $filter_order;
			// Table ordering End.

			// Search filter
			$lists['search']	= $search;
			// Search fiter End

			// assign values here.
			$this->assignRef('items',		$rows);
			$this->assignRef('pageNav',		$pageNav);
			$this->assignRef('lists',		$lists);

			parent::display($tpl);
		}
	}
}
